/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.graphics.region;


private import dwt.dwt;



private import dwt.graphics.color;
private import dwt.graphics.cursor;
private import dwt.graphics.device;
private import dwt.graphics.devicedata;
private import dwt.graphics.drawable;
private import dwt.graphics.font;
private import dwt.graphics.fontdata;
private import dwt.graphics.fontmetrics;
private import dwt.graphics.gc;
private import dwt.graphics.gcdata;
private import dwt.graphics.image;
private import dwt.graphics.imagedata;
private import dwt.graphics.imageloader;
private import dwt.graphics.imageloaderevent;
private import dwt.graphics.imageloaderlistener;
private import dwt.graphics.palettedata;
private import dwt.graphics.point;
private import dwt.graphics.rectangle;
private import dwt.graphics.rgb;
private import dwt.graphics.textlayout;
private import dwt.graphics.textstyle;
private import dwt.internal.win32.os;


/**
 * Instances of this class represent areas of an x-y coordinate
 * system that are aggregates of the areas covered by a number
 * of rectangles.
 * <p>
 * Application code must explicitly invoke the <code>Region.dispose()</code> 
 * method to release the operating system resources managed by each instance
 * when those instances are no longer required.
 * </p>
 */

public class Region {

	/**
	 * the OS resource for the region
	 * (Warning: This field is platform dependent)
	 */
	public HRGN handle;
	
	/**
	 * the device where this region was created
	 */
	Device device;

/**
 * Constructs a new empty region.
 * 
 * @exception SWTError <ul>
 *    <li>ERROR_NO_HANDLES if a handle could not be obtained for region creation</li>
 * </ul>
 */
public this () {
	this(null);
}

/**
 * Constructs a new empty region.
 * <p>
 * You must dispose the region when it is no longer required. 
 * </p>
 *
 * @param device the device on which to allocate the region
 *
* @exception SWTError <ul>
 *    <li>ERROR_NO_HANDLES if a handle could not be obtained for region creation</li>
 * </ul>
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
 * </ul>
 *
 * @see #dispose
 * 
 * @since 3.0
 */
public this (Device device) {
	if (device is null) device = Device.getDevice();
	if (device is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	this.device = device;
	handle = OS.CreateRectRgn (0, 0, 0, 0);
	if (handle is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NO_HANDLES);
	debug
	{
	if (device.tracking) device.new_Object(this);
	}
}

/**
 * Constructs a new region given a handle to the operating
 * system resources that it should represent.
 * 
 * @param handle the handle for the result
 */
this(Device device, HRGN handle) {
	this.device = device;
	this.handle = handle;
}

/**
 * Adds the given polygon to the collection of rectangles
 * the receiver maintains to describe its area.
 *
 * @param pointArray points that describe the polygon to merge with the receiver
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 *
 * @since 3.0
*
 */
public void add (int[] pointArray) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (pointArray is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	version(WinCE) { DWT.error(__FILE__, __LINE__, DWT.ERROR_NOT_IMPLEMENTED); }
	HRGN polyRgn = OS.CreatePolygonRgn(cast(POINT*)pointArray, pointArray.length / 2, OS.ALTERNATE);
	OS.CombineRgn (handle, handle, polyRgn, OS.RGN_OR);
	OS.DeleteObject (polyRgn);
}

/**
 * Adds the given rectangle to the collection of rectangles
 * the receiver maintains to describe its area.
 *
 * @param rect the rectangle to merge with the receiver
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 */
public void add (Rectangle rect) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (rect is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (rect.width < 0 || rect.height < 0) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	
	HRGN rectRgn = OS.CreateRectRgn (rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
	OS.CombineRgn (handle, handle, rectRgn, OS.RGN_OR);
	OS.DeleteObject (rectRgn);
}

/**
 * Adds all of the rectangles which make up the area covered
 * by the argument to the collection of rectangles the receiver
 * maintains to describe its area.
 *
 * @param region the region to merge
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 */
public void add (Region region) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (region is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (region.isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	OS.CombineRgn (handle, handle, region.handle, OS.RGN_OR);
}

/**
 * Returns <code>true</code> if the point specified by the
 * arguments is inside the area specified by the receiver,
 * and <code>false</code> otherwise.
 *
 * @param x the x coordinate of the point to test for containment
 * @param y the y coordinate of the point to test for containment
 * @return <code>true</code> if the region contains the point and <code>false</code> otherwise
 *
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 */
public boolean contains (int x, int y) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if( OS.PtInRegion (handle, x, y))
		return true;
	return false;
}

/**
 * Returns <code>true</code> if the given point is inside the
 * area specified by the receiver, and <code>false</code>
 * otherwise.
 *
 * @param pt the point to test for containment
 * @return <code>true</code> if the region contains the point and <code>false</code> otherwise
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 */
public boolean contains (Point pt) {
	if (pt is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	return contains(pt.x, pt.y);
}

/**
 * Disposes of the operating system resources associated with
 * the region. Applications must dispose of all regions which
 * they allocate.
 */
public void dispose () {
	if (handle is null) return;
	if (device.isDisposed()) return;
	OS.DeleteObject(handle);
	handle = null;
	debug
	{
	if (device.tracking) device.dispose_Object(this);
	}
	device = null;
}

/**
 * Compares the argument to the receiver, and returns true
 * if they represent the <em>same</em> object using a class
 * specific comparison.
 *
 * @param object the object to compare with this object
 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
 *
 * @see #hashCode
 */
public boolean equals (Object object) {
	if (this is object) return true;
	if (!(cast(Region)object)) return false;
	Region rgn = cast(Region)object;
	return handle is rgn.handle;
}

/**
 * Returns a rectangle which represents the rectangular
 * union of the collection of rectangles the receiver
 * maintains to describe its area.
 *
 * @return a bounding rectangle for the region
 *
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 *
 * @see Rectangle#union
 */
public Rectangle getBounds() {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	RECT rect;
	OS.GetRgnBox(handle, &rect);
	return new Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}

/**
 * Returns an integer hash code for the receiver. Any two 
 * objects which return <code>true</code> when passed to 
 * <code>equals</code> must return the same value for this
 * method.
 *
 * @return the receiver's hash
 *
 * @see #equals
 */
public int hashCode () {
	return cast(int)handle;
}

/**
 * Intersects the given rectangle to the collection of rectangles
 * the receiver maintains to describe its area.
 *
 * @param rect the rectangle to intersect with the receiver
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 * 
 * @since 3.0
 */
public void intersect (Rectangle rect) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (rect is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (rect.width < 0 || rect.height < 0) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	
	HRGN rectRgn = OS.CreateRectRgn (rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
	OS.CombineRgn (handle, handle, rectRgn, OS.RGN_AND);
	OS.DeleteObject (rectRgn);
}

/**
 * Intersects all of the rectangles which make up the area covered
 * by the argument to the collection of rectangles the receiver
 * maintains to describe its area.
 *
 * @param region the region to intersect
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 * 
 * @since 3.0
 */
public void intersect (Region region) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (region is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (region.isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	OS.CombineRgn (handle, handle, region.handle, OS.RGN_AND);
}

/**
 * Returns <code>true</code> if the rectangle described by the
 * arguments intersects with any of the rectangles the receiver
 * mainains to describe its area, and <code>false</code> otherwise.
 *
 * @param x the x coordinate of the origin of the rectangle
 * @param y the y coordinate of the origin of the rectangle
 * @param width the width of the rectangle
 * @param height the height of the rectangle
 * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
 *
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 *
 * @see Rectangle#intersects
 */
public boolean intersects (int x, int y, int width, int height) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	RECT r;
	OS.SetRect (&r, x, y, x + width, y + height);
	if( OS.RectInRegion (handle, &r))
		return true;
	return false;
}

/**
 * Returns <code>true</code> if the given rectangle intersects
 * with any of the rectangles the receiver mainains to describe
 * its area and <code>false</code> otherwise.
 *
 * @param rect the rectangle to test for intersection
 * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 *
 * @see Rectangle#intersects
 */
public boolean intersects (Rectangle rect) {
	if (rect is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	return intersects(rect.x, rect.y, rect.width, rect.height);
}

/**
 * Returns <code>true</code> if the region has been disposed,
 * and <code>false</code> otherwise.
 * <p>
 * This method gets the dispose state for the region.
 * When a region has been disposed, it is an error to
 * invoke any other method using the region.
 *
 * @return <code>true</code> when the region is disposed, and <code>false</code> otherwise
 */
public boolean isDisposed() {
	return handle is null;
}

/**
 * Returns <code>true</code> if the receiver does not cover any
 * area in the (x, y) coordinate plane, and <code>false</code> if
 * the receiver does cover some area in the plane.
 *
 * @return <code>true</code> if the receiver is empty, and <code>false</code> otherwise
 *
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 */
public boolean isEmpty () {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	RECT rect;
	int result = OS.GetRgnBox (handle, &rect);
	if (result == OS.NULLREGION) return true;
	return ((rect.right - rect.left) <= 0) || ((rect.bottom - rect.top) <= 0);
}

/**
 * Subtracts the given polygon from the collection of rectangles
 * the receiver maintains to describe its area.
 *
 * @param pointArray points that describe the polygon to merge with the receiver
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 * 
 * @since 3.0
 */
public void subtract (int[] pointArray) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (pointArray is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	version(WinCE) { DWT.error(__FILE__, __LINE__, DWT.ERROR_NOT_IMPLEMENTED); }
	HRGN polyRgn = OS.CreatePolygonRgn(cast(POINT*)pointArray, pointArray.length / 2, OS.ALTERNATE);
	OS.CombineRgn (handle, handle, polyRgn, OS.RGN_DIFF);
	OS.DeleteObject (polyRgn);
}

/**
 * Subtracts the given rectangle from the collection of rectangles
 * the receiver maintains to describe its area.
 *
 * @param rect the rectangle to subtract from the receiver
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 * 
 * @since 3.0
 */
public void subtract (Rectangle rect) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (rect is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (rect.width < 0 || rect.height < 0) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	
	HRGN rectRgn = OS.CreateRectRgn (rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
	OS.CombineRgn (handle, handle, rectRgn, OS.RGN_DIFF);
	OS.DeleteObject (rectRgn);
}

/**
 * Subtracts all of the rectangles which make up the area covered
 * by the argument from the collection of rectangles the receiver
 * maintains to describe its area.
 *
 * @param region the region to subtract
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
 *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
 * </ul>
 * 
 * @since 3.0
 */
public void subtract (Region region) {
	if (isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_GRAPHIC_DISPOSED);
	if (region is null) DWT.error(__FILE__, __LINE__, DWT.ERROR_NULL_ARGUMENT);
	if (region.isDisposed()) DWT.error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
	OS.CombineRgn (handle, handle, region.handle, OS.RGN_DIFF);
}

/**	 
 * Invokes platform specific functionality to allocate a new region.
 * <p>
 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
 * API for <code>Region</code>. It is marked public only so that it
 * can be shared within the packages provided by DWT. It is not
 * available on all platforms, and should never be called from
 * application code.
 * </p>
 *
 * @param device the device on which to allocate the region
 * @param handle the handle for the region
 * @return a new region object containing the specified device and handle
 */
public static Region win32_new(Device device, HRGN handle) {
	return new Region(device, handle);
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the receiver
 */
public char[] toString() {
	
	if (isDisposed()) return ("Region {*DISPOSED*}");
	char[] s =  "Region {"  ~ Int.toString(cast(uint)handle) ~ "}";
	return s;
}

}
